﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Web.Configuration;
using System.Configuration;
using System.Web;
using System.Data.SqlClient;
using System.Collections;
using DTN.Core.Utilities;

namespace DTN.Core.Common
{
    public class AppEnv
    {
        public static string BIDURL
        {
            get { return WebConfigurationManager.AppSettings["BIDURL"]; }
        }

        /// <summary>
        /// 
        /// </summary>
        
       
        public static string ConnectionString
        {
            get { return GetConnectionString("ConnString"); }
        }
      
        
        public static string DomainCookie
        {
            get { return GetAppSetting("DomainCookie"); }
        }
        public static string Domain
        {
            get { return ConvertUtility.ToString(GetAppSetting("Domain")); }
        }


        public static string GetConnectionString(string _key)
        {
            return WebConfigurationManager.ConnectionStrings[_key].ConnectionString;
        }

        public static string GetAppSetting(string key)
        {
            if (System.Configuration.ConfigurationSettings.AppSettings[key] != null)
                return System.Configuration.ConfigurationSettings.AppSettings[key];
            else return string.Empty;
        }

        public static string GetAppSetting(string key, string configFileName)
        {
            XmlDocument xd = new XmlDocument();
            xd.Load(HttpContext.Current.Server.MapPath(configFileName));
            XmlNode node = xd.DocumentElement.SelectSingleNode("/configuration/appSettings/add[@key=\"" + key + "\"]");
            if (node != null) return node.Attributes.GetNamedItem("value").Value;
            else return null;
        }



        public static object GetSection(string _name)
        {
            return WebConfigurationManager.GetSection(_name);
        }

        public static XmlDocument AddAppSetting(XmlDocument xmlDoc, string _key, string _val)
        {
            //XmlElement xmlSetting;
            XmlNode appSettingNode = xmlDoc.SelectSingleNode("//appSettings");
            if (appSettingNode != null)
            {
                XmlElement xmlSetting = (XmlElement)appSettingNode.SelectSingleNode("//add[@key='" + _key + "']");
                if (xmlSetting != null)
                {
                    xmlSetting.SetAttribute("value", _val);
                }
                else
                {
                    xmlSetting = xmlDoc.CreateElement("add");
                    xmlSetting.SetAttribute("key", _key);
                    xmlSetting.SetAttribute("value", _val);
                    appSettingNode.AppendChild(xmlSetting);
                }
            }
            return xmlDoc;
        }
        public static XmlDocument LoadConfig(string _fileName)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load((_fileName));
            return xmlDoc;
        }
        public static string SaveConfig(string _fileName, XmlDocument xmlDoc)
        {
            try
            {
                XmlTextWriter writer = new XmlTextWriter((_fileName), Encoding.Unicode);
                writer.Formatting = Formatting.Indented;
                xmlDoc.WriteTo(writer);
                writer.Flush();
                writer.Close();
                return string.Empty;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }

        public static string CurrentDomain
        {
            get
            {
                HttpContext context = HttpContext.Current;
                string host = context.Request.IsLocal ?
                    context.Request.Url.Host + ":" + context.Request.Url.Port :
                    context.Request.Url.Host;
                return host;
            }
        }
    }
}
